<template>
  <div class="login-container">
    <div class="login-wrapper">
      <div class="login-logo">
        <h1>高校职工就餐系统</h1>
        <p>便捷的校园餐饮服务平台</p>
      </div>
      
      <div class="login-form">
        <h2>用户登录</h2>
        <form @submit.prevent="handleLogin">
          <div class="form-group">
            <label for="employeeId">工号</label>
            <div class="input-wrapper">
              <svg class="input-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path>
                <circle cx="12" cy="7" r="4"></circle>
              </svg>
              <input 
                type="text" 
                id="employeeId" 
                v-model="loginForm.employeeId" 
                placeholder="请输入工号"
                required
                autocomplete="off"
              />
            </div>
          </div>
          
          <div class="form-group">
            <label for="password">密码</label>
            <div class="input-wrapper">
              <svg class="input-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect>
                <path d="M7 11V7a5 5 0 0 1 10 0v4"></path>
              </svg>
              <input 
                :type="showPassword ? 'text' : 'password'" 
                id="password" 
                v-model="loginForm.password" 
                placeholder="请输入密码"
                required
                autocomplete="off"
              />
              <button 
                type="button" 
                class="toggle-password" 
                @click="showPassword = !showPassword"
              >
                {{ showPassword ? '隐藏' : '显示' }}
              </button>
            </div>
          </div>
          
          <div class="form-options">
            <label class="remember-me">
              <input type="checkbox" v-model="rememberMe" />
              记住我
            </label>
            <a href="#" class="forgot-password">忘记密码？</a>
          </div>
          
          <button type="submit" class="login-btn" :disabled="isLoading">
            {{ isLoading ? '登录中...' : '登录' }}
          </button>
          
          <div class="register-link">
            还没有账号？<router-link to="/register">立即注册</router-link>
          </div>
        </form>
      </div>
    </div>
    
    <div class="login-footer">
      <p>© 2025 高校职工就餐系统 版权所有</p>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'

export default {
  name: 'LoginPage',
  setup() {
    const router = useRouter()
    const isLoading = ref(false)
    const showPassword = ref(false)
    const rememberMe = ref(false)
    
    const loginForm = reactive({
      employeeId: '',
      password: ''
    })
    
    const handleLogin = async () => {
      isLoading.value = true
      
      try {
        // 模拟API请求
        console.log('登录请求:', loginForm)
        
        // 模拟网络延迟
        await new Promise(resolve => setTimeout(resolve, 1000))
        
        // 模拟登录逻辑（实际项目中应替换为真实的API调用）
        if (loginForm.employeeId && loginForm.password) {
          // 构造用户信息
          const userInfo = {
            employeeId: loginForm.employeeId,
            // 这里可以添加更多用户信息
            loginTime: new Date().toISOString()
          }
          
          // 保存用户信息到localStorage
          if (rememberMe.value) {
            localStorage.setItem('user', JSON.stringify(userInfo))
          } else {
            sessionStorage.setItem('user', JSON.stringify(userInfo))
          }
          
          // 登录成功后跳转到首页
          router.push('/')
        } else {
          alert('请填写完整的登录信息')
        }
      } catch (error) {
        console.error('登录失败:', error)
        alert('登录失败，请检查职工编号和密码')
      } finally {
        isLoading.value = false
      }
    }
    
    return {
      loginForm,
      isLoading,
      showPassword,
      rememberMe,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-wrapper {
  width: 100%;
  max-width: 800px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.login-logo {
  text-align: center;
  padding: 30px;
  background: #f8f9fa;
  border-bottom: 1px solid #e9ecef;
}

.login-logo h1 {
  color: #3a3f51;
  margin-bottom: 10px;
  font-size: 28px;
}

.login-logo p {
  color: #6c757d;
  font-size: 16px;
}

.login-form {
  padding: 40px;
}

.login-form h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #3a3f51;
}

.form-group {
  margin-bottom: 25px;
}

label {
  display: block;
  margin-bottom: 8px;
  color: #495057;
  font-weight: 500;
  font-size: 14px;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 15px;
  color: #adb5bd;
  width: 20px;
  height: 20px;
}

input {
  width: 100%;
  padding: 12px 15px 12px 45px;
  border: 1px solid #ced4da;
  border-radius: 6px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: #fff;
}

input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.toggle-password {
  position: absolute;
  right: 15px;
  background: none;
  border: none;
  color: #6c757d;
  cursor: pointer;
  font-size: 14px;
  padding: 5px;
}

.toggle-password:hover {
  color: #495057;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
}

.remember-me {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #495057;
  font-size: 14px;
}

.remember-me input {
  width: auto;
  margin-right: 8px;
  padding: 0;
}

.forgot-password {
  color: #667eea;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s;
}

.forgot-password:hover {
  color: #5a67d8;
  text-decoration: underline;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
}

.login-btn:disabled {
  background: #adb5bd;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #495057;
  font-size: 14px;
}

.register-link a {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
}

.register-link a:hover {
  text-decoration: underline;
}

.login-footer {
  margin-top: 40px;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-form {
    padding: 30px 20px;
  }
  
  .login-logo h1 {
    font-size: 24px;
  }
  
  .login-logo p {
    font-size: 14px;
  }
}
</style>